package com.seckill.utils;

import org.apache.commons.codec.digest.DigestUtils;

public class MD5Util {
    /**
     *  md5加密
     * @param src
     * @return
     */
    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }
    private static final String salt = "1a2b3c4d";

    /**
     * 前端输入密码MD5+salt加密
     * @param inputPass
     * @return
     */
    public static String inputPassToFormPass(String inputPass) {
        // 1 2 c 3
        String str = "" + salt.charAt(0) + salt.charAt(2) + inputPass + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }
    /**
     * 前端加密的密码在进行md5+slat加密
     * @param formPass
     * @param salt
     * @return
     */
    public static String formPassToDBPass(String formPass, String salt) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + formPass + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    /**
     * 后端使用的加密方法MD5((MD5+slat)+slat)
     * @param inputPass
     * @param salt
     * @return
     */
     public static String inputPassToDBPass(String inputPass, String salt) {
        String formPass = inputPassToFormPass(inputPass);
        String dbPass = formPassToDBPass(formPass, salt);
        return dbPass;
    }

    public static void main(String[] args) {
         System.out.println(inputPassToFormPass("123456"));
         System.out.println(formPassToDBPass(inputPassToFormPass("123456"), "1a2b3c4d"));
         System.out.println(inputPassToDBPass("123456", "1a2b3c4d"));
    }
}
